package com.purong.purong.net;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.RequestParams;
import com.purong.purong.Contant;
import com.purong.purong.bean.ImgUpLoadBean;
import com.purong.purong.util.FileUtil;
import com.purong.purong.util.SharePreUtil;
import com.yao.sdk.encrypt.Encrypt;

import java.io.File;
import java.io.FileNotFoundException;

import cz.msebera.android.httpclient.Header;

/**
 * Created by wenjiezeng on 2017/10/16.
 */
public class UpLoadImg {
    //存储目录【avatar：个人头像,identity：身份证,portrait:肖像】[path]
    public static final String UPLOAD_AVATAR="avatar",UPLOAD_IDENTITY="identity",UPLOAD_PORTRAIT="portrait";
    private Context mContext;
    private String path;
    private boolean needScal=false;
    private OnUploadImgListener mListener;
    private String type=UPLOAD_AVATAR;
    private int minSize=300;
    public UpLoadImg(Context context,String path,String type,OnUploadImgListener listener){
        this.mContext=context;
        this.path=path;
        this.mListener=listener;
        if (!type.equals(UPLOAD_AVATAR)&&!type.equals(UPLOAD_IDENTITY)&&!type.equals(UPLOAD_PORTRAIT)){
            this.type=UPLOAD_AVATAR;
        }else {
            this.type=type;
        }
    }
    public void setNeedScal(int minSize){
        this.needScal=true;
        this.minSize=minSize;
    }
    public void setNeedScal(){
        this.needScal=true;
    }
    public void upLoad(){
        RequestParams params = new RequestParams();

        try {
            if (needScal&&!(path.endsWith(".gif")||path.endsWith(".GIF"))){
                params.put("file", FileUtil.scal(mContext,path,minSize));
            }else {
                params.put("file", new File(path));
            }

            params.put("path",type);
            params.put("_t", SharePreUtil.getInstance(mContext).getToken());

        } catch (FileNotFoundException e) {
            if (mListener!=null)
                mListener.onFail("未找到文件",e);
            e.printStackTrace();
        }
        HttpUtil.getInstance().sendPost(Contant.UPLOAD_IMG,params,new ImgHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                super.onSuccess(statusCode, headers, responseString);
                Log.e("zwj","...upload success");
                Log.e("zwj","...responseString="+responseString);
                try {
                    responseString=new Encrypt().toDecryption(responseString);
                    ImgUpLoadBean bean= JSON.parseObject(responseString,ImgUpLoadBean.class);
                    if (mListener!=null){
                        if (bean.get_code().equals("0")){
                            mListener.onSuccess(responseString,bean);
                        }else {
                            mListener.onFail(bean.get_msg(),null);
                        }
                    }


                    Log.e("zwj","..url="+bean.getUrl());
                    Log.e("zwj","..weburl="+bean.getWeb_url());
                }catch (Exception e){
                    if (mListener!=null)
                        mListener.onFail(responseString,null);
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
//                    hander.sendEmptyMessage(-2);
                if (mListener!=null)
                    mListener.onFail(responseString,throwable);
                Log.e("zwj","...upload Fail");
            }
        });
    }
    public interface OnUploadImgListener{
        void onSuccess(String responseString, ImgUpLoadBean bean);
        void onFail(String responseString, Throwable throwable);
    }
}
